package com.googlecode.garbagecan.springsecuritystudy.acl;

import org.springframework.security.acls.MutableAcl;
import org.springframework.security.acls.MutableAclService;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.security.acls.objectidentity.ObjectIdentity;
import org.springframework.security.acls.objectidentity.ObjectIdentityImpl;
import org.springframework.security.acls.sid.PrincipalSid;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ContractServiceImpl implements ContractService {
	private List<Contract> list = new ArrayList<Contract>();
	private MutableAclService mutableAclService;

	public Contract get(Long id) {
		for (Contract contract : list) {
			if (contract.getId().equals(id)) {
				return contract;
			}
		}
		return null;
	}

	public List<Contract> getAll() {
		return list;
	}

	public void save(String contractContent, String owner) {
		Contract contract = new Contract();
		contract.setId(System.currentTimeMillis());
		contract.setContent(contractContent);
		contract.setOwner(owner);
		contract.setCreateDate(new Date());
		contract.setUpdateDate(new Date());
		list.add(contract);

		ObjectIdentity oid = new ObjectIdentityImpl(Contract.class, contract.getId());
		MutableAcl acl = mutableAclService.createAcl(oid);
		acl.insertAce(0, BasePermission.ADMINISTRATION, new PrincipalSid(owner), true);
	}

	public void update(Long id, String contractContent) {
		Contract contract = this.get(id);
		contract.setContent(contractContent);
		contract.setUpdateDate(new Date());
	}

	public void removeById(Long id) {
		Contract contract = this.get(id);
		this.remove(contract);
	}

	public void remove(Contract contract) {
		list.remove(contract);
		ObjectIdentity oid = new ObjectIdentityImpl(Contract.class, contract.getId());
		mutableAclService.deleteAcl(oid, false);
	}

	public void setMutableAclService(MutableAclService mutableAclService) {
		this.mutableAclService = mutableAclService;
	}
}
